import { EventEmitter, Injectable } from '@angular/core';
import { Http, URLSearchParams } from "@angular/http";
import { Observable } from "rxjs";
import 'rxjs/Rx';

@Injectable ()
export class ProductService {

    /*private products : Product[] = [
     {
     id : 1,
     title : '第一个商品',
     price : 1.99,
     rating : 3,
     desc : '466666666我在学习angular5',
     cateqories : ['cssScanner', 'ts']
     },
     {
     id : 2,
     title : '第2个商品',
     price : 2.99,
     rating : 1,
     desc : '我在学习angular5',
     cateqories : ['cssScanner', 'ts']
     },
     {
     id : 3,
     title : '第3个商品',
     price : 3.99,
     rating : 4,
     desc : '22222222我在学习angular5',
     cateqories : ['cssScfdgdfanner', 'ts']
     },
     {
     id : 4,
     title : '第4个商品',
     price : 1.99,
     rating : 3,
     desc : '我在学习5555555angular5',
     cateqories : ['432342', 'ts23452345']
     },
     {
     id : 5,
     title : '第5个商品',
     price : 3.99,
     rating : 3.5,
     desc : '我在学习angular5555555555',
     cateqories : ['cssSc3456anner', 't34563s']
     }
     ];*/
    /*private comments : Comment[] = [
     {
     id : 1,
     title : '第一个商品',
     timestamp : '2018/03/27 11:31:25',
     rating : 3,
     user : '瑞恩',
     content : '东西还可以的哦!'
     },
     {
     id : 2,
     title : '第2个商品',
     timestamp : '2018/03/22 11:31:25',
     rating : 3,
     user : '瑞444444恩',
     content : '东西还444444可以的哦!'
     },
     {
     id : 3,
     title : '第3个商品',
     timestamp : '2018/04/27 11:31:25',
     rating : 1,
     user : '瑞44恩',
     content : '东西还GDDFGHDFGHD可以的哦!'
     },
     {
     id : 4,
     title : '第4个商品',
     timestamp : '2018/03/24 11:34:25',
     rating : 4,
     user : '瑞4444恩',
     content : '东西还可以的哦9999999!'
     },
     ];*/

    constructor ( private http : Http ) {
    }

    searchEvent : EventEmitter<ProductSearchParams> = new EventEmitter ();

    getAllCate () : string[] {
        return ['电子产品', '硬件设备', '图书']
    }

    getProducts () : Observable<Product[]> {
        return this.http.get ('/api/products')
            .map (res => res.json ());
    }

    getProduct ( id : number ) : Observable<Product> {
        return this.http.get ('/api/product/' + id)
            .map (res => res.json ());
    }

    getCommentsForProduct ( id : number ) : Observable<Comment[]> {
        return this.http.get ('/api/product/' + id + '/comments')
            .map (res => res.json ());
    }

    search ( params : ProductSearchParams ) : Observable<Product[]> {
        return this.http.get ('/api/products', { search : this.encodeParams (params) })
            .map (res => res.json ());
    }

    private encodeParams ( params : ProductSearchParams ) {
        return Object.keys (params)
            .filter (key => params[key])
            .reduce (( sum : URLSearchParams, key : string ) => {
                sum.append (key, params[key]);
                return sum;
            }, new URLSearchParams ());


    }

}

export class ProductSearchParams {
    constructor ( public title : string,
                  public price : number,
                  public category : string ) {
    }
}


export class Product {
    constructor ( public id : number,
                  public title : string,
                  public  price : number,
                  public rating : number,
                  public  desc : string,
                  public  cateqories : Array<string> ) {
    }
}

export class Comment {
    constructor ( public id : number,
                  public protectId : number,
                  public timestamp : string,
                  public rating : number,
                  public user : string,
                  public content : string ) {
    }
}